package com.itheima.studentmanagementsystem;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 班级类应该包含学生列表和添加学生的方法。
 * 使用单例设计模式确保每个班级只有一个实例。
 */
public class Classroom {
    /*  public static ArrayList<Student> classRoomList = new ArrayList<>();

      private Classroom() {

      }

      public static ArrayList<Student> getClassRoom() {
          return classRoomList;
      }

      public static ArrayList<Student> setStudent(ArrayList<Student> listId) {
          System.out.println("请添加学生信息");
          Scanner s = new Scanner(System.in);
          Student student = new Student();

          System.out.println("请输入学生姓名");
          student.setName(s.next());
          System.out.println("请输入学生学号");
          String id = s.next();
          boolean flag = judge(listId, id);
          if (flag == true) {
              student.setId(id);
              System.out.println("请输入学生年龄");
              student.setAge(s.nextInt());
          }
          ArrayList<Student> list = new ArrayList<>();
          list.add(student);
          return list;


      }

      public static void printStudent(ArrayList<Student> c1) {
          if (c1 == null) {
              System.out.println("当前系统中学生信息为零，请先输入学生信息再查询学生信息");
              return;
          }
          for (int i = 0; i < c1.size(); i++) {
              System.out.println(c1.get(i).toString());
          }
      }
  */
    //判断输入的学号是否重复
    public static boolean judge(ArrayList<Student> listId, String id) {
        for (int i = 0; i < listId.size(); i++) {
            if (listId.get(i).getId().equals(id)) {
                System.out.println("您输入的学号重复，请重新输入");
                return false;
            }
        }
        return true;
    }

    public static ArrayList<Student> classRoomList;

    private Classroom() {

    }

    //--------
    public static ArrayList<Student> getClassroom() {
        if (classRoomList == null) {
            classRoomList = new ArrayList<>();
        }
        return classRoomList;
    }
    //----------

    public static ArrayList<Student> getStudents() {
        return classRoomList;
    }

    public static void setStudents(ArrayList<Student> classRoomList) {
        Classroom.classRoomList = classRoomList;
    }

    //添加学生对象
    public static ArrayList<Student> setStudent(ArrayList<Student> listId) {
        System.out.println("添加学生信息");
        Scanner s = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        Student student = new Student();
        System.out.println("请输入姓名");
        student.setName(s.next());
        System.out.println("请输入学号");
        String id = s.next();
        if (judge(listId, id) == true) {
            student.setId(id);
            System.out.println("请输入年龄");
            student.setAge(s.nextInt());
        }


        list.add(student);
        return list;
    }

    //打印学生列表
    public static void printStudent(ArrayList<Student> list) {
        if (list == null) {
            System.out.println("当前系统的学生信息为零，请添加学生信息后再查看学生列表！");
            setStudents(list);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }
    }


}
